import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

// import { h } from 'vue';
import { DictEnum } from '@vben/constants';
import { VbenIcon } from '@vben/icons';

import { SafetyCertificateTwoTone } from '@ant-design/icons-vue';
import { Tooltip } from 'ant-design-vue';

// import { getPopupContainer } from '@vben/utils';
import { z } from '#/adapter/form';
import ColorTagToggleList from '#/components/tag/src/color-tag-toggle-list.vue';
import { getDictOptions } from '#/utils/dict';
// import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'name',
    label: '企业名称',
  },
  {
    component: 'Select',
    fieldName: 'certificationStatus',
    componentProps: {
      options: getDictOptions(DictEnum.CERTIFICATION_STATUS, true),
    },
    label: '认证状态',
  },
  {
    component: 'UserListSelect',
    fieldName: 'followUpId',
    label: '跟进人',
  },
  // {
  //   component: 'TagSearchFilter',
  //   fieldName: 'tagInfo',
  //   label: '企业标签',
  //   componentProps: { tagType: 1 }, // 1企业 2人员
  //   defaultValue: [[], ''],
  // },
  // {
  //   component: 'TagSearchFilter',
  //   fieldName: 'tagInfo2',
  //   label: '人员标签',
  //   componentProps: { tagType: 2 },
  //   defaultValue: [[], ''],
  // },
  // {
  //   component: 'AddressCascader',
  //   fieldName: 'addressMap',
  //   label: '所在地区',
  //   defaultValue: ['00', '44', '4403', '440303'],
  // },
  // {
  //   component: 'CompanyTagSelect',
  //   fieldName: 'tagInfo3',
  //   label: '标签测试',
  //   defaultValue: ['1944671790434201602', '1943228088267366402'],
  // },
];

export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '企业名称',
    field: 'name',
    width: 300,
    slots: {
      default: ({ row }) => {
        return (
          <div class="flex items-center justify-start gap-1">
            <span>
              {row?.website ? (
                <Tooltip title={row.website}>
                  <a
                    class="mt-[6px] inline-block"
                    href={row.website}
                    target="_blank"
                  >
                    <VbenIcon icon="mynaui:link-solid" />
                  </a>
                </Tooltip>
              ) : (
                ''
              )}
            </span>
            <span>{row.name}</span>
            {row?.certificationStatus === 1 ? (
              <div class="inline-block">
                <SafetyCertificateTwoTone title="已认证" />
              </div>
            ) : (
              ''
            )}
          </div>
        );
      },
    },
  },
  {
    title: '企业标签',
    field: 'labels',
    minWidth: 300,
    slots: {
      default: ({ row }) => {
        return <ColorTagToggleList labels={row.labels} />;
      },
    },
  },
  {
    title: '企业地址',
    field: 'countryCode',
    width: 400,
    visible: false,
    slots: {
      default: ({ row }) => {
        if (row.isAbroad === 1) {
          return (
            <span>
              {row.address} {row.countryName}
            </span>
          );
        }
        return (
          <span>
            {row.countryName}
            {row.provinceName}
            {row.cityName}
            {row.districtName}
            {row.address}
          </span>
        );
      },
    },
  },
  {
    title: '人员数量',
    field: 'employeeCount',
    width: 90,
  },
  {
    title: '所属来源',
    field: 'sourceName',
    width: 160,
    visible: false,
  },
  {
    title: '跟进人',
    field: 'followUpName',
    width: 140,
  },
  {
    title: '最近联系时间',
    field: 'lastContactTime',
    width: 170,
    showOverflow: true,
  },
  // {
  //   title: '企业简介',
  //   field: 'introduction',
  //   showOverflow: true,
  // },
  {
    title: '创建人',
    field: 'createByName',
    width: 140,
    visible: false,
  },
  {
    title: '创建时间',
    field: 'createTime',
    width: 180,
    visible: false,
  },
  {
    title: '最后更新人',
    field: 'updateByName',
    width: 140,
    visible: false,
  },
  {
    title: '最后更新时间',
    field: 'updateTime',
    width: 180,
    visible: false,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 'auto',
  },
];

export const drawerSchema: FormSchemaGetter = () => [
  {
    label: '企业ID',
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '认证状态',
    fieldName: 'certificationStatus',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    defaultValue: 0, // 默认未认证
  },
  {
    label: '企业名称',
    fieldName: 'name',
    component: 'TycCompanySearchSelect',
    defaultValue: null,
    help: '输入企业名称后将通过天眼查接口进行模糊查询',
    rules: 'required',
  },
  {
    label: '企业简称',
    fieldName: 'alias',
    component: 'Input',
    // rules: 'required',
  },
  {
    label: '所属来源',
    fieldName: 'sourceId',
    component: 'SourceSelect',
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      options: getDictOptions(DictEnum.COMPANY_TYPE, true),
      optionType: 'button',
    },
    fieldName: 'type',
    label: '企业类型',
    rules: 'selectRequired',
  },
  {
    component: 'CompanyTagSelect',
    fieldName: 'labelIds',
    label: '企业标签',
    defaultValue: [],
    help: '可多选，通过筛选批量设置企业标签',
    rules: 'selectRequired',
  },
  {
    component: 'RadioGroup',
    componentProps(model) {
      return {
        buttonStyle: 'solid',
        options: getDictOptions(DictEnum.BUSINESS_YES_NO, true),
        optionType: 'button',
        onChange: () => {
          model.addressMap = [];
        },
      };
    },
    fieldName: 'isAbroad',
    label: '是否海外',
    defaultValue: 0,
    help: '用于区分国内和国外企业，如选择为是，所在地区需选择海外国家',
    rules: 'selectRequired',
  },
  {
    component: 'AddressCascader',
    fieldName: 'addressMap',
    label: '所在地区',
    defaultValue: [],
    rules: 'selectRequired',
    dependencies: {
      componentProps(values) {
        return {
          isAbroad: values.isAbroad,
        };
      },
      triggerFields: ['isAbroad'],
    },
    help: '如需选择海外国家，请先将「是否海外」选为「是」',
  },
  {
    label: '详细地址',
    fieldName: 'address',
    component: 'Input',
    // rules: 'required',
    help: '海外企业请填写英文地址',
  },
  {
    component: 'Input',
    fieldName: 'website',
    help: `网址链接为http://或https://开头，如https://www.iteschina.com`,
    label: '企业网址',
    rules: z
      .string({ message: '请输入链接地址' })
      .regex(/^https?:\/\//, { message: '请输入正确的链接地址' })
      .optional()
      .or(z.literal('')),
  },
  // {
  //   component: 'RadioGroup',
  //   componentProps: {
  //     buttonStyle: 'solid',
  //     options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
  //     optionType: 'button',
  //   },
  //   defaultValue: '0',
  //   fieldName: 'status',
  //   label: '来源状态',
  //   rules: 'selectRequired',
  // },
  {
    label: '企业简介',
    fieldName: 'introduction',
    component: 'Textarea',
    componentProps: {
      autoSize: { minRows: 3 },
      showCount: true,
      maxlength: 400,
    },
  },
  {
    label: '备注',
    fieldName: 'remark',
    component: 'Textarea',
    componentProps: {
      showCount: true,
      maxlength: 100,
    },
  },
];

// 转移跟进人
export const transferSchema: FormSchemaGetter = () => [
  {
    label: '企业IDS',
    fieldName: 'ids',
    component: 'Select',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    rules: 'selectRequired',
  },
  {
    component: 'UserListSelect',
    fieldName: 'followUpId',
    label: '跟进人',
    rules: 'selectRequired',
  },
];

// 企业合并
export const mergeSchema: FormSchemaGetter = () => [
  {
    label: '企业IDS',
    fieldName: 'ids',
    component: 'Select',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    rules: 'selectRequired',
  },
  {
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      options: [],
      optionType: 'button',
    },
    fieldName: 'id',
    label: '合并至',
    help: '请选择一个合并后保留数据的企业',
    rules: 'selectRequired',
  },
];
